<?php
namespace Admin\Controller;
use Tools\AdminController,    Model\LinkModel,
    Model\Link_classModel;

class LinkController extends AdminController {
    
    function showlist(){
        //链接展示
        $lkmd = new LinkModel();
        $lcmd = new Link_classModel();
        
        $flist = $lcmd -> order('link_class_sort asc') -> select();
        $list  = $lkmd -> order('link_sort asc') ->select();
        
        $this -> assign('list',$list);
        $this -> assign('flist',$flist);
        $this -> display();
    }
   
    
    function add(){
        $lkmd = new LinkModel();
        $lcmd = new Link_classModel();
        
        //链接分类
        $class = $lcmd -> select(); 
       
        if(!empty($_POST)){
            
            //数据存储
            $shu = $lkmd -> create($_POST);
            if($shu){
                if($lkmd -> add()){
                    $this ->redirect('showlist');
                }else {
                    echo "添加失败";
                }
            }else{
                $error = $lkmd -> getError();
                $this  -> alert($error); 
            }           
        }
        $this -> assign('class',$class);
        $this -> display();
    }
    
    
    function delete($link_id){
        $lkmd = new LinkModel();

       if($lkmd->delete($str)){
           $this -> redirect('showlist');
       }else{
           $this -> redirect('showlist','',3,'删除失败');
       }  
    }
    
    
    function xiugai($link_id){
        $lkmd = new LinkModel();       
        $lcmd = new Link_classModel();
        
        //链接分类
        $class = $lcmd -> select(); 
        
        $list = $lkmd -> find($link_id);
        
        //收集修改信息
        if(!empty($_POST)){
           
            $shu = $lkmd -> create($_POST);
            if($shu){
                if($lkmd -> save()){
                    $this ->redirect('showlist');
                }else {
                    $this ->redirect('showlist');
                }
            }else{
                echo $lkmd -> getError();
            }  
        }
        
        $this -> assign('class',$class);
        $this -> assign('list',$list);
        $this -> display();
    }
    
    
    function change($id,$sort){
        $lkmd = new LinkModel();
        
        $up['link_id']   = $id;
        $up['link_sort'] = $sort;
        
        $shu = $lkmd -> create($up);
        if($shu){
            $lkmd -> save($shu);
        }else{
            echo $lkmd -> getError();
        }  
    }
    
}